package com.qijian.maindata.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qijian.common.annotation.Log;
import com.qijian.common.core.controller.BaseController;
import com.qijian.common.core.domain.AjaxResult;
import com.qijian.common.core.page.TableDataInfo;
import com.qijian.common.enums.BusinessType;
import com.qijian.common.utils.mybatis.WhereEntityTool;
import com.qijian.common.utils.poi.ExcelUtil;
import com.qijian.maindata.domain.ExamineSchemeDetails;
import com.qijian.maindata.domain.query.ExamineSchemeDetailsQuery;
import com.qijian.maindata.service.IExamineSchemeDetailsService;
import io.swagger.annotations.Api;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 检验方案明细Controller
 *
 * @author qijian
 * @date 2021-11-24
 */
@Api(tags = "质检管理：检验方案明细")
@RestController
@RequiredArgsConstructor
@RequestMapping("/maindata/details")
public class ExamineSchemeDetailsController extends BaseController {

    private final IExamineSchemeDetailsService examineSchemeDetailsService;

    /**
     * 查询检验方案明细列表
     */
    @PreAuthorize("@ss.hasPermi('maindata:details:list')")
    @GetMapping("/list")
    public TableDataInfo list(ExamineSchemeDetailsQuery query, IPage<ExamineSchemeDetails> page) {
        return getDataTable(examineSchemeDetailsService.page(Page.of(page.getCurrent(), page.getSize()), WhereEntityTool.invoke(query)));
    }

    @GetMapping("/list/all")
    public TableDataInfo list(ExamineSchemeDetailsQuery query) {
        return getDataTable(examineSchemeDetailsService.vo(WhereEntityTool.invoke(query)));
    }

    /**
     * 导出检验方案明细列表
     */
    @PreAuthorize("@ss.hasPermi('maindata:details:export')")
    @Log(title = "检验方案明细", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public void export(ExamineSchemeDetailsQuery query, HttpServletResponse response) {
        new ExcelUtil<>(ExamineSchemeDetails.class).exportExcel(response, examineSchemeDetailsService.list(WhereEntityTool.invoke(query)), "检验方案明细数据");
    }

    /**
     * 获取检验方案明细详细信息
     */
    @PreAuthorize("@ss.hasPermi('maindata:details:query')")
    @GetMapping(value = "/{detailId}")
    public AjaxResult getInfo(@PathVariable("detailId") Long detailId) {
        return AjaxResult.success(examineSchemeDetailsService.getById(detailId));
    }

    /**
     * 新增检验方案明细
     */
    @PreAuthorize("@ss.hasPermi('maindata:details:add')")
    @Log(title = "检验方案明细", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ExamineSchemeDetails examineSchemeDetails) {
        return toAjax(examineSchemeDetailsService.save(examineSchemeDetails));
    }

    /**
     * 修改检验方案明细
     */
    @PreAuthorize("@ss.hasPermi('maindata:details:edit')")
    @Log(title = "检验方案明细", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ExamineSchemeDetails examineSchemeDetails) {
        return toAjax(examineSchemeDetailsService.updateById(examineSchemeDetails));
    }

    /**
     * 删除检验方案明细
     */
    @PreAuthorize("@ss.hasPermi('maindata:details:remove')")
    @Log(title = "检验方案明细", businessType = BusinessType.DELETE)
    @DeleteMapping("/{detailIds}")
    public AjaxResult remove(@PathVariable List<Long> detailIds) {
        return toAjax(examineSchemeDetailsService.removeByIds(detailIds));
    }
}
